const mysql = require('mysql');
const config = require('./../../config/config.local.js');

var connectionPool = mysql.createPool({
  'host' : config.database.host,
  'port':config.database.port,
  'user' : config.database.user,
  'password' : config.database.password,
  'database' : config.database.database,
  'charset': config.database.charset,
  'connectionLimit': config.database.connectionLimit,
  'supportBigNumbers': true,
  'bigNumberStrings': true
});

var release = connection => {
  connection.end(function(error) {
    if(error) {
      console.log('Connection closed failed.');
    } else {
      console.log('Connection closed succeeded.');
    }
  });
};

var execQuery = sqlOptions => {
  var results = new Promise((resolve, reject) => {
      connectionPool.getConnection((error,connection) => {
        if(error) {
          console.log("Get connection from mysql pool failed !");
          reject(error);
        }

        var sql = sqlOptions['sql'];
        var args = sqlOptions['args'];

        var query = connection.query(sql, args, function(error, results) {
            if(error) {
              console.log('Execute query error !');
              reject(error);
            }
            resolve(results);
        });

        connection.release(function(error) {
          if(error) {
            console.log('Mysql connection close failed !');
            reject(error);
          }
        });
    });
  }).then(function (chunk) {
    return chunk;
  }).catch(function(error){
    console.log(error);
  });

  return results;
};

module.exports = {
  release : release,
  execQuery : execQuery
}

